home *** CD-ROM | disk | FTP | other *** search
/ C & C++ Multimedia Cyber Classroom / C and C++ Multimedia Cyber Classroom (Prentice Hall) (1998).iso / src / fig02_09.jar / Ch02 / Fig02_09 / Fig02_09.cpp
C/C++ Source or Header  |  1997-10-11  |  1KB  |  41 lines

  1. // Fig. 2.9: fig02_09.cpp
  2. // Class average program with sentinel-controlled repetition.
  3. #include <iostream.h>
  4. #include <iomanip.h>
  5.  
  6. int main()
  7. {
  8.    int total,        // sum of grades
  9.        gradeCounter, // number of grades entered
  10.        grade;        // one grade       
  11.    float average;    // number with decimal point for average
  12.  
  13.    // initialization phase
  14.    total = 0;
  15.    gradeCounter = 0;
  16.  
  17.    // processing phase
  18.    cout << "Enter grade, -1 to end: ";    
  19.    cin >> grade;                         
  20.  
  21.    while ( grade != -1 ) {                  
  22.       total = total + grade;              
  23.       gradeCounter = gradeCounter + 1;              
  24.       cout << "Enter grade, -1 to end: "; 
  25.       cin >> grade;                       
  26.    }
  27.  
  28.    // termination phase
  29.    if ( gradeCounter != 0 ) {     
  30.       average = static_cast< float >( total ) / gradeCounter;   
  31.       cout << "Class average is " << setprecision( 2 )
  32.            << setiosflags( ios::fixed | ios::showpoint )
  33.            << average << endl;
  34.    }
  35.    else
  36.       cout << "No grades were entered" << endl;
  37.  
  38.    return 0;   // indicate program ended successfully
  39. }
  40.  
  41.